package com.fin.zw.intelligentbot.controller;


import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.entity.QVariable;
import com.fin.zw.intelligentbot.entity.Variable;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/ai/variable")
@Api(value = "客户变量", description = "客户变量")
public class VariableController {

    @Autowired
    private BaseService<Variable> variableService;

    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的客户变量", notes = "查询所有的客户变量")
    public ResponseEntity<List<Variable>> getAiAccountList(@ApiIgnore @CurrentUser SessionModel sessionModel,
                                                             Variable condition){
        List<Variable> list = this.variableService.fetch(sessionModel,condition, QVariable.variable);
        return ResponseEntity.ok().body(list);
    }

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的客户变量", notes = "查询所有的客户变量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<Variable>> fetchPage(Pageable pageable, @ApiIgnore @CurrentUser SessionModel sessionModel,
                                                    Variable condition) {
        Page<Variable> page = this.variableService.fetchPage(pageable, sessionModel, condition, QVariable.variable);
        return ResponseEntity.ok().body(page);
    }


    @RequestMapping(value = "/update",method = {RequestMethod.PUT,RequestMethod.PATCH})
    @ApiOperation(value = "更新分类标签",notes = "更新分类标签")
    public ResponseEntity<String> update(@RequestBody Variable variable,
                                                @ApiIgnore @CurrentUser SessionModel sessionModel){
        this.variableService.update(variable,sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加分类标签",notes = "添加分类标签")
    public ResponseEntity<String> add(@RequestBody Variable variable,
                                      @ApiIgnore @CurrentUser SessionModel sessionModel){
        this.variableService.add(variable,sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "客户分类标签",notes = "客户分类标签")
    public ResponseEntity<String> delete(@PathVariable String id){
        this.variableService.delete(id);
        return ResponseEntity.ok().body("操作成功");
    }
}
